home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
9-Digit Zip Code Directory
/
9-Digit Zip Code Directory (American Business Information) (ABIZIP-12).ISO
/
z4src.zip
/
CPASCII.C
< prev
next >
Wrap
C/C++ Source or Header
|
1993-07-06
|
10KB
|
445 lines
//----------------------------------------------------------------------------
// MODULE DESCRIPTION
//
// Module: cpascii.c
// Title: Compress Library
// Notice: John M. Weeder
// Copyright (c) 1993. All rights reserved.
// This module contains proprietary information and should be
// treated as confidential.
//
//----------------------------------------------------------------------------
// MAINTENANCE HISTORY
//
// $Workfile$
// $Revision$
// $Author$
// $Date$
// $Log$
//
//----------------------------------------------------------------------------
// MODULE NARRATIVE
//
//
// This module contains code to do a simple ascii compression technique. 8 bit
// ASCII codes are converted into packed 6 bit codes.
//
// All lower case characters are converted to upper case.
// The following characters are ignored:
// ASCII 1..31 (Control characters)
// Underscore
// Tilde
// Caret
// Bar |
// Back quote `
// Del (127)
// Backslash
//
// One advantage of this method is that ASCII sort order is preserved in the
// encoded string.
//
// The code in this module should be written entirely in C.
// Do not use any C++ constructs.
//
// This module is portable to:
// DOS 3.X+
// MS Windows 3.X+
// OS/2 2.X+
// OS/2 2.0 PM
// SCO UNIX.
//
// The following compilers are supported:
// MSC 6.0A
// MSC/C++ 7.0
// Borland C++ 3.1 for DOS
// Borland C++ 1.0 for OS/2 2.X
// SCO UNIX cc
//
//----------------------------------------------------------------------------
#include <cp.h>
//----------------------------------------------------------------------------
// Data
//----------------------------------------------------------------------------
static BYTE bAscii2Encoded[128] =
{
0, /* 0 ' ' */
255, /* 1 ' ' */
255, /* 2 ' ' */
255, /* 3 ' ' */
255, /* 4 ' ' */
255, /* 5 ' ' */
255, /* 6 ' ' */
255, /* 7 ' ' */
255, /* 8 ' ' */
255, /* 9 ' ' */
255, /* 10 ' ' */
255, /* 11 ' ' */
255, /* 12 ' ' */
255, /* 13 ' ' */
255, /* 14 ' ' */
255, /* 15 ' ' */
255, /* 16 ' ' */
255, /* 17 ' ' */
255, /* 18 ' ' */
255, /* 19 ' ' */
255, /* 20 ' ' */
255, /* 21 ' ' */
255, /* 22 ' ' */
255, /* 23 ' ' */
255, /* 24 ' ' */
255, /* 25 ' ' */
255, /* 26 ' ' */
255, /* 27 ' ' */
255, /* 28 ' ' */
255, /* 29 ' ' */
255, /* 30 ' ' */
255, /* 31 ' ' */
1, /* 32 ' ' */
2, /* 33 '!' */
3, /* 34 '"' */
4, /* 35 '#' */
5, /* 36 '$' */
6, /* 37 '%' */
7, /* 38 '&' */
8, /* 39 ''' */
9, /* 40 '(' */
10, /* 41 ')' */
11, /* 42 '*' */
12, /* 43 '+' */
13, /* 44 ',' */
14, /* 45 '-' */
15, /* 46 '.' */
16, /* 47 '/' */
17, /* 48 '0' */
18, /* 49 '1' */
19, /* 50 '2' */
20, /* 51 '3' */
21, /* 52 '4' */
22, /* 53 '5' */
23, /* 54 '6' */
24, /* 55 '7' */
25, /* 56 '8' */
26, /* 57 '9' */
27, /* 58 ':' */
28, /* 59 ';' */
29, /* 60 '<' */
30, /* 61 '=' */
31, /* 62 '>' */
32, /* 63 '?' */
33, /* 64 '@' */
34, /* 65 'A' */
35, /* 66 'B' */
36, /* 67 'C' */
37, /* 68 'D' */
38, /* 69 'E' */
39, /* 70 'F' */
40, /* 71 'G' */
41, /* 72 'H' */
42, /* 73 'I' */
43, /* 74 'J' */
44, /* 75 'K' */
45, /* 76 'L' */
46, /* 77 'M' */
47, /* 78 'N' */
48, /* 79 'O' */
49, /* 80 'P' */
50, /* 81 'Q' */
51, /* 82 'R' */
52, /* 83 'S' */
53, /* 84 'T' */
54, /* 85 'U' */
55, /* 86 'V' */
56, /* 87 'W' */
57, /* 88 'X' */
58, /* 89 'Y' */
59, /* 90 'Z' */
60, /* 91 '[' */
255, /* 92 '\' */
61, /* 93 ']' */
255, /* 94 '^' */
255, /* 95 '_' */
255, /* 96 '`' */
34, /* 97 'a' */
35, /* 98 'b' */
36, /* 99 'c' */
37, /* 100 'd' */
38, /* 101 'e' */
39, /* 102 'f' */
40, /* 103 'g' */
41, /* 104 'h' */
42, /* 105 'i' */
43, /* 106 'j' */
44, /* 107 'k' */
45, /* 108 'l' */
46, /* 109 'm' */
47, /* 110 'n' */
48, /* 111 'o' */
49, /* 112 'p' */
50, /* 113 'q' */
51, /* 114 'r' */
52, /* 115 's' */
53, /* 116 't' */
54, /* 117 'u' */
55, /* 118 'v' */
56, /* 119 'w' */
57, /* 120 'x' */
58, /* 121 'y' */
59, /* 122 'z' */
62, /* 123 '{' */
255, /* 124 '|' */
63, /* 125 '}' */
255, /* 126 '~' */
255, /* 127 ' ' */
};
static BYTE bEncoded2Ascii[64] =
{
'\0',
' ',
'!',
'"',
'#',
'$',
'%',
'&',
'\'',
'(',
')',
'*',
'+',
',',
'-',
'.',
'/',
'0',
'1',
'2',
'3',
'4',
'5',
'6',
'7',
'8',
'9',
':',
';',
'<',
'=',
'>',
'?',
'@',
'A',
'B',
'C',
'D',
'E',
'F',
'G',
'H',
'I',
'J',
'K',
'L',
'M',
'N',
'O',
'P',
'Q',
'R',
'S',
'T',
'U',
'V',
'W',
'X',
'Y',
'Z',
'[',
']',
'{',
'}',
};
//----------------------------------------------------------------------------
// Description: Decode a buffer
// Parameters: pbDecode Pointer to buffer to decode.
// pcDecode Pointer to recieve number of bytes decoded.
// pch Buffer to place decoded text into.
// Null terminator is added to buffer.
// Returns: TRUE if successful.
//----------------------------------------------------------------------------
BOOL FN_E AsciiDecode(PBYTE pbDecode, PSIZET pcDecode, PCHAR pch)
{
SIZET cDecode = 0;
SIZET cBitsLeft = 0;
BYTE bVal, bBits, bInput;
Assert(pbDecode && pcDecode && pch);
for (;;)
{
if (cBitsLeft < 6)
{
bInput = *pbDecode++;
cDecode++;
}
switch (cBitsLeft)
{
case 0:
bVal = (BYTE)(bInput >> 2);
bBits = bInput;
cBitsLeft = 2;
break;
case 2:
bVal = (BYTE)((bBits << 4) | (bInput >> 4));
bBits = bInput;
cBitsLeft = 4;
break;
case 4:
bVal = (BYTE)((bBits << 2) | (bInput >> 6));
bBits = bInput;
cBitsLeft = 6;
break;
case 6:
bVal = bBits;
cBitsLeft = 0;
break;
} // Decode it
*pch = (CHAR)bEncoded2Ascii[bVal & 0x3F];
if (*pch == '\0') // Decoded end of line?
break;
pch++; // Move to next character
}
*pcDecode = cDecode;
return TRUE;
}
//----------------------------------------------------------------------------
// Description: Encode a buffer
// Parameters: pbEncode Pointer to buffer to encode into.
// pcEncode Pointer to receive size of encoded text.
// psz Buffer to read text from.
// Returns: TRUE if successful.
//----------------------------------------------------------------------------
BOOL FN_E AsciiEncode(PBYTE pbEncode, PSIZET pcEncode, PCSZ pcsz)
{
SIZET cEncode = 0;
SIZET cBitsLeft = 8;
BYTE bBits = 0;
BYTE bVal, bOutput;
Assert(pbEncode && pcEncode && pcsz);
for (;;)
{
if ((BYTE)pcsz[0] >= 128)
bVal = 255;
else
bVal = bAscii2Encoded[(BYTE)pcsz[0]];
if (bVal != 255)
{
switch (cBitsLeft)
{
case 2:
bOutput = (bBits | (bVal >> 4));
bBits = (BYTE)(bVal << 4);
cBitsLeft = 4;
break;
case 4:
bOutput = (bBits | (bVal >> 2));
bBits = (BYTE)(bVal << 6);
cBitsLeft = 6;
break;
case 6:
bOutput = (bBits | bVal);
bBits = 0;
cBitsLeft = 8;
break;
case 8:
bBits = (BYTE)(bVal << 2);
cBitsLeft = 2;
break;
}
if (cBitsLeft != 2)
{
*pbEncode++ = bOutput; // Output
cEncode++;
}
}
if (!pcsz[0]) // End of string?
break; // Yes, done!
pcsz++; // Move to next character
}
if (cBitsLeft != 8) // Stuff left over bits into buffer
{
*pbEncode = bBits; // Place into buffer
cEncode++;
} // Return number of bytes used
*pcEncode = cEncode;
return TRUE;
}
//----------------------------------------------------------------------------
// Description: Run standard test suite
// Parameters: sTest Test to run.
// 0 Run all default tests (except).
// Returns: TRUE if successful.
//----------------------------------------------------------------------------
#if COMPILE_TEST
BOOL FN AsciiTest(SHORT sTest)
{
static BYTE bBuf[512];
static SIZET cBuf;
static CHAR szInput[512];
static CHAR szResult[512];
SIZET i;
if (sTest == 0 || sTest == 1)
{
static PSZ pszTest1 = "abc";
if (!AsciiEncode(bBuf, &cBuf, pszTest1))
return FALSE;
if (cBuf != (((strlen(pszTest1) + 1) * 6) + 6) / 8)
return FALSE;
if (!AsciiDecode(bBuf, &cBuf, szResult))
return FALSE;
if (stricmp(szResult, pszTest1) != 0)
return FALSE;
}
if (sTest == 0 || sTest == 2)
{
memset(szInput, 0, sizeof(szInput));
for (i = 1; i < 256; ++i)
szInput[i - 1] = (CHAR)i;
if (!AsciiEncode(bBuf, &cBuf, szInput))
return FALSE;
if (!AsciiDecode(bBuf, &cBuf, szResult))
return FALSE;
if (strlen(szResult) != 63 + 26)
return FALSE;
}
return TRUE;
}
#endif
//----------------------------------------------------------------------------
//------------------------------- End of File --------------------------------
//----------------------------------------------------------------------------